package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.common.DelStatus;
import com.youi.service.ActivitygoodsService;
import com.youi.service.GoodsService;
import com.youi.service.GoodsimgsService;
import com.youi.entity.Activitygoods;
import com.youi.entity.Goods;
import com.youi.entity.Goodsimgs;
import com.youi.entity.Goodssize;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 活动商品信息控制层
* @author Administrator
*/
@Controller
public class ActivitygoodsController  extends HttpRequestManage{

		@Autowired
		private ActivitygoodsService activitygoodsService;
		@Autowired
		private GoodsService goodsService;
		@Autowired
		private GoodsimgsService goodsimgsService; 
		/**
		 * I-49
		 *添加或编辑活动商品信息
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/addOrEditActivitygoods")
		public String addOrEditActivitygoods(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Activitygoods activitygoods=null;
			if (id!=null) {
				activitygoods=activitygoodsService.get(id);
			}else {
				activitygoods=new Activitygoods();
				activitygoods.setDelStatus(DelStatus.NODEL);
				activitygoods.setGmtCreated(new Date());
				activitygoods.setActivityId(1L);
			}	
			//商品id
			Long goodsId=optLong("goodsId",request);
			if (id==null) {
				if (goodsId!=null){				
					Goods goods=goodsService.get(goodsId);
					if (goods==null) {
						return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "商品信息为空");
					}
					Map<String, Object> params=new HashMap<String, Object>();
					params.put(Common.DELSTATUS, DelStatus.NODEL);
					params.put("goodsId", goodsId);
					Activitygoods otherActivitygoods=activitygoodsService.getByParam(params);
					if (otherActivitygoods!=null) {
						return JsonUtil.toJson(CommonInfoUtil.OBJECTEXIST,"无法重复添加活动商品");
					}
					goodsService.getGoodsMsg(goods);
					Goodssize goodssize=goods.getGoodssizeList().get(0);
					activitygoods.setGoodsId(goodsId);					
					activitygoods.setGoodsName(goods.getName());
					activitygoods.setGoodsNo(goods.getGoodsNo());
					activitygoods.setCollagePrice(goodssize.getPrice());
					activitygoods.setCostPrice(goods.getEcPrice());
					activitygoods.setRealCostPrice(goods.getCostPrice());
				}
			}
			//活动商品图片
			String goodsImg=optString("goodsImg", request);
			if (goodsImg!=null) {
				activitygoods.setGoodsImg(goodsImg);
			}
			//虚拟库存数
			Integer stockCnt=optInteger("stockCnt",request);
			if (stockCnt!=null){
				activitygoods.setStockCnt(stockCnt);
			}
			//虚拟已售数
			Integer buyCnt=optInteger("buyCnt",request);
			if (buyCnt!=null){
				activitygoods.setBuyCnt(buyCnt);
			}
			//配货数
			Integer disGoodsCnt=optInteger("disGoodsCnt",request);
			if (disGoodsCnt!=null){
				activitygoods.setDisGoodsCnt(disGoodsCnt);
			}
			//真实库存数
			Integer realStockCnt=optInteger("realStockCnt",request);
			if (realStockCnt!=null){
				activitygoods.setRealStockCnt(realStockCnt);
			}
			Integer sort=optInteger("sort", request);
			if (sort!=null) {
				activitygoods.setSort(sort);
			}
			activitygoods.setGmtModify(new Date());
			activitygoodsService.saveOrUpdate(activitygoods);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-51
		 * 删除活动商品信息
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/delActivitygoodsByIds")
		public String delActivitygoodsByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Activitygoods> activitygoodsList= activitygoodsService.getByParams(params);
			if (activitygoodsList==null||activitygoodsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Activitygoods activitygoods : activitygoodsList) {
				activitygoods.setDelStatus(DelStatus.DEL);
			}
			activitygoodsService.saveOrUpdateAll(activitygoodsList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-50
		 * 查询活动商品信息列表
		 * @param modelMap
		 * @param request
		 * @return
		 * @throws BusinessException
		 * @throws ParseException
		 */
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getActivitygoods")
		public String getActivitygoods(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			String goodsPms=optString("goodsPms",request);
			String start=optString("start",request);
			String end=optString("end",request);
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			if (goodsPms!=null&&goodsPms.trim().length()>0) {
				List<Long> goodsIdList=activitygoodsService.getGoodsIdByPms(goodsPms);
				if (goodsIdList!=null&&goodsIdList.size()>0) {
					prams.put("goodsId", goodsIdList);
				}
			}
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put("sort", des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,DateUtil.simpdfyMdHms.parse(start));
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,DateUtil.simpdfyMdHms.parse(end));
			}
			List<Activitygoods> activitygoodsList=activitygoodsService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (activitygoodsList==null||activitygoodsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=activitygoodsService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, activitygoodsList);		//jsonObjectList json对象集合
			activitygoodsService.getActivitygoodsMsg(activitygoodsList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 * I-51
		 * 查询活动商品信息详情
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getActivitygoodsById")
		public String getActivitygoodsById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Activitygoods activitygoods=activitygoodsService.get(id);
			if (activitygoods==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",activitygoods);//jsonObject	json对象
		}

		/**
		 * 
		 * 查询所有活动商品信息
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllActivitygoods")
		public String getAllActivitygoods(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long activityId=getLong("activityId", request);
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put("activityId", activityId);
			List<Activitygoods> activitygoodsList= activitygoodsService.getByParams(params);
			if (activitygoodsList==null||activitygoodsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",activitygoodsList);//jsonObjectList	json对象集合
		}
}
